package com.lw.yygh.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lw.yygh.enums.RefundStatusEnum;
import com.lw.yygh.model.order.PaymentInfo;
import com.lw.yygh.model.order.RefundInfo;
import com.lw.yygh.order.mapper.RefundMapper;
import com.lw.yygh.order.service.RefundService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RefundServiceImpl extends ServiceImpl<RefundMapper, RefundInfo> implements RefundService {

    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        //判断是否有重复记录添加
        QueryWrapper<RefundInfo> wrapper=new QueryWrapper<>();
        wrapper.eq( "out_trade_no",paymentInfo.getOutTradeNo() );
        wrapper.eq( "payment_type",paymentInfo.getPaymentType() );
        RefundInfo refundInfo = baseMapper.selectOne( wrapper );
        if (refundInfo!=null){
            //有重复数据
            return refundInfo;
        }else {
            //添加记录
            refundInfo = new RefundInfo();
            refundInfo.setCreateTime(new Date());
            refundInfo.setOrderId(paymentInfo.getOrderId());
            refundInfo.setPaymentType(paymentInfo.getPaymentType());
            refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
            refundInfo.setRefundStatus( RefundStatusEnum.UNREFUND.getStatus());
            refundInfo.setSubject(paymentInfo.getSubject());;
            refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
            baseMapper.insert(refundInfo);
            return refundInfo;
        }
    }
}
